package com.xuetang9.t9.hotel.services.impl;

import com.xuetang9.t9.hotel.beans.EntryFood;
import com.xuetang9.t9.hotel.beans.EntryOrder;
import com.xuetang9.t9.hotel.mapper.EntryFoodMapper;
import com.xuetang9.t9.hotel.mapper.EntryOrderMapper;
import com.xuetang9.t9.hotel.services.IEntryOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Classname EntryFoodService
 * @Description TODO
 * @Date 2020/2/24 16:28
 * @Created by ASUS
 */
@Service
public class EntryOrderService implements IEntryOrderService {

    @Autowired
    private EntryOrderMapper entryOrderMapper;

    @Autowired
    private EntryFoodMapper entryFoodMapper;

    @Override
    public boolean addEntry(EntryOrder entryOrder) {
        entryOrderMapper.insertEntry(entryOrder);
        Long entryOrderId = entryOrder.getId();
        if(entryOrder.getEntryFoods() != null) {
            entryOrder.getEntryFoods().forEach((entryFood) -> {
                entryFoodMapper.insertEntryFood(entryOrderId, entryFood.getFoodId(), entryFood.getCount());
            });
        }
        return entryOrderId != null;
    }

    @Override
    public boolean delEntry(Long id) {
        entryFoodMapper.removeEntryFood(id);
        int removeResult = entryOrderMapper.removeEntry(id);
        return removeResult > 0;
    }

    @Override
    public EntryOrder toUp(Long id) {
        EntryOrder entryOrder = entryOrderMapper.queryById(id);
        List<EntryFood> entryFoods = entryFoodMapper.queryByEntryId(id);
        entryOrder.setEntryFoods(entryFoods);
        return entryOrder;
    }

    @Override
    public boolean updateEntry(EntryOrder entryOrder) {
        entryOrderMapper.updateEntry(entryOrder);
        if(entryOrder.getEntryFoods() != null){
            entryOrder.getEntryFoods().forEach((entryFood)->{
                entryFoodMapper.updateEntryFood(entryFood);
            });
        }
        return false;
    }

    @Override
    public List<EntryOrder> queryByNowDate(String nowDate) {
        return entryOrderMapper.queryByDate(nowDate);
    }
}
